package com.imooc.miaosha.controller;

import com.imooc.miaosha.domain.MiaoShaUser;
import com.imooc.miaosha.domain.User;
import com.imooc.miaosha.rabbitmq.MQSender;
import com.imooc.miaosha.rabbitmq.MiaoshaMessage;
import com.imooc.miaosha.redis.RedisService;
import com.imooc.miaosha.redis.UserKey;
import com.imooc.miaosha.result.Result;
import com.imooc.miaosha.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/")
public class SimpleDemo {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private MQSender mqSender;

    @GetMapping
    public String simpleDemo(Model model){

        model.addAttribute("hello","DevTools");

        return "Demo";
    }

    @GetMapping("/dbTest")
    @ResponseBody
    public Result<User> testDb(){
        User user = userService.selectById(1);

        return Result.<User>success(user);
    }

    @GetMapping("/test2")
    public Result<Boolean> testTx(){
        return Result.success(userService.tx());
    }

    @GetMapping("/redisget")
    @ResponseBody
    public  Result<Long> redisGetTest(){
        redisService.decr(UserKey.getById,"wyl");
        Long key1 = redisService.get(UserKey.getById,"wyl", Long.class);
        return Result.success(key1);
    }

    @GetMapping("/redisset")
    @ResponseBody
    public boolean redisSetTest(){
        redisService.set(UserKey.getById,"wyl",170788);
        return true;
    }

    @GetMapping("/exits")
    @ResponseBody
    public boolean testExits(){

        return redisService.exits(UserKey.getById,"wyl");
    }

 /*   @GetMapping("/rabbit33")
    @ResponseBody
    public String testRabbit33(){
        MiaoshaMessage miaoshaMessage = new MiaoshaMessage();
        MiaoShaUser user = new MiaoShaUser();
        user.setId(33);
        miaoshaMessage.setMiaoShaUser(user);
        mqSender.sendMiaoshaMessage(miaoshaMessage);
        return "hello!Rabbit";
    }*/

    /*@GetMapping("/rabbit")
    @ResponseBody
    public String testRabbitMQ(){
        mqSender.send("Hello 钟佳明！");
        return "Hello,RabbitMQ";
    }

    @GetMapping("/topic")
    @ResponseBody
    public String testTopic(){
        mqSender.topicSend("佳明1收到一条消息，佳明2收到一条消息");
        return "Test Topic!";
    }

    @GetMapping("/fanout")
    @ResponseBody
    public String testFanout(){
        mqSender.fanoutSend("这是一条广播，三个佳明都有消息");
        return "Test Fanout!";
    }

    @GetMapping("/headers")
    @ResponseBody
    public String testHeaders(){
        mqSender.headersSend("3收到消息");
        return "Test Headers";
    }*/
}
